package guanli;

import java.util.ArrayList;
import java.util.Scanner;

public class Stusystem {
    public static void main(String[] args) {
        ArrayList<Stu> list = new ArrayList<Stu>();
        while (true) {
            System.out.println("Welcome to Student System");
            System.out.println("1.添加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.查询学生信息");
            System.out.println("5.退出系统");
            System.out.println("请输入您的选择：");
            Scanner scanner = new Scanner(System.in);
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    addStu(list);
                    // break;
                case "2":
                    delStu(list);
                    //break;
                case "3":
                    updateStu(list);
                    // break;
                case "4":
                    queryStu(list);
                    // break;
                case "5":
                    System.out.println("退出系统");
                    System.exit(0);
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }

    }

    public static void addStu(ArrayList<Stu> list) {
        Scanner sc = new Scanner(System.in);

        System.out.println("添加学生id：");
        String id = sc.next();

        boolean flag = contains(list, id);

        if (flag) {
            System.out.println("该学生id已存在，请重新输入！");
        } else {
            System.out.println("添加学生id：");
        }

        System.out.println("添加学生姓名：");
        String name = sc.next();

        System.out.println("添加学生年龄：");
        int age = sc.nextInt();

        System.out.println("添加学生地址：");
        String address = sc.next();

        Stu s = new Stu(id, name, age, address);
        list.add(s);
        System.out.println("添加成功！");
    }

    public static void delStu(ArrayList<Stu> list) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要删除的学生id：");
        String id = sc.next();

        int index = getIndex(list, id);

        if (index >= 0) {
            list.remove(index);
            System.out.println("id为" + id + "的学生信息已删除！");
        } else {
            System.out.println("该学生id不存在！");
        }
    }

    public static void updateStu(ArrayList<Stu> list) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要修改的学生id：");
        String id = sc.next();

        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("该学生id不存在！");
            return;
        }

        Stu stu = list.get(index);

        System.out.println("请输入修改后的学生姓名：");
        String name = sc.next();
        stu.setName(name);

        System.out.println("请输入修改后的学生年龄：");
        int age = sc.nextInt();
        stu.setAge(age);

        System.out.println("请输入修改后的学生地址：");
        String address = sc.next();
        stu.setAddress(address);

        System.out.println("修改成功！");
    }


    public static void queryStu(ArrayList<Stu> list) {
        System.out.println("查询学生信息");
        if (list.size() == 0) {
            System.out.println("暂无学生信息,请添加学生信息！");
            return;
        }
        System.out.println("编号\t姓名\t年龄\t地址");
        for (int i = 0; i < list.size(); i++) {
            Stu stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());

        }
    }

    public static boolean contains(ArrayList<Stu> list, String id) {
//        for (int i = 0; i < list.size(); i++) {
//            Stu stu=list.get(i);
//            String stuId=stu.getId();
//            if(stuId.equals(id)){
//                return true;
//            }
//        }
//        return false;
        return getIndex(list, id) >= 0;
    }

    public static int getIndex(ArrayList<Stu> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Stu stu = list.get(i);
            String stuId = stu.getId();
            if (stuId.equals(id)) {
                return i;
            }
        }
        return -1;
    }
}


